<template>
  <div class="sms-notice">
    <titleBar titleText="消息通知" :hasBottomBorder="true">
      <moreDropdownMenu :routerConfig="routerListCofig"></moreDropdownMenu>
    </titleBar>
    <ul style="margin-bottom: 30px;">
      <li class="notice" v-for="item in smsList" :key="item.id">
        <span class="bell"></span>
        <div
          :class="{readed: !!item.isRead}"
          class="notice-msg"
          @click="lookDetail(item)"
        >
          <ellipsisForText :fileName="item.content">
            <span style="margin-left: 16px;">{{ item.createdAt }}</span>
          </ellipsisForText>
        </div>
      </li>
    </ul>
    <!-- 分页模块 -->
    <el-pagination
      align="right"
      class="el-pagination-style"
      @size-change="sizeChange"
      @current-change="pageChange"
      :page-sizes="PAGE_NUMBER"
      :page-size="pageSize"
      :current-page="curPage"
      :layout="PAGINATION_LAYOUT"
      :total="totalElements"
    />
  </div>
</template>

<script>
import titleBar from '@/moduleComponents/newWorkbench/titleBar';
import ellipsisForText from '@/moduleComponents/newWorkbench/ellipsisForText';
import moreDropdownMenu from '@/moduleComponents/newWorkbench/MoreDropdownMenu';
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common';
import {
  workbenchInformationNewNoticeList,
  workbenchInformationNoticepdateIsRead
} from '@const/mDataCenterApi.js';
export default {
  name: 'SmsNotice',
  components: {
    titleBar,
    ellipsisForText,
    moreDropdownMenu
  },
  data() {
    return {
      smsList: [],
      routerListCofig: [
        {
          path: '/todoNoticeSet',
          label: '待办通知设置'
        }
      ],
      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0 // 总共的条数
    };
  },
  created() {
    this.getSmsList();
  },
  methods: {
    // 获取消息通知列表
    getSmsList() {
      this.$axios({
        url: workbenchInformationNewNoticeList,
        method: 'post',
        data: {
          page: this.curPage,
          pageSize: this.pageSize,
          ignoreStatus: 0
        }
      })
        .then(res => {
          this.smsList = res.data.payload.content || [];
          this.totalElements = res.data.payload.totalElements;
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 页码更改
    sizeChange(size) {
      this.pageSize = size;
      this.getSmsList();
    },
    // 切换不一样的页面,重置一切数据
    pageChange(curPage) {
      this.curPage = curPage;
      this.getSmsList();
    },
    // 设置为已读
    setRead(id, menuUrl) {
      this.$axios({
        url: workbenchInformationNoticepdateIsRead + `/${id}`,
        method: 'get'
      })
        .then(res => {
          if (res.data.code === '0') {
            this.getSmsList();
            if (menuUrl === 'null' || !menuUrl) {
              this.$message.warning('没有有效的跳转链接!');
              return;
            }
            window.open(menuUrl);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 查看
    lookDetail({ menuUrl, id, isRead }) {
      if (isRead) {
        if (menuUrl === 'null' || !menuUrl) {
          this.$message.warning('没有有效的跳转链接!');
          return;
        }
        window.open(menuUrl);
        return;
      }
      this.setRead(id, menuUrl);
    }
  }
};
</script>

<style scoped lang="less">
.el-icon-more {
  font-size: 20px;
  cursor: pointer;
  height: 100%;
  margin-right: 8px;
}

.sms-notice ul {
  padding: 0;
  margin: 0;
  list-style: none;
  max-height: 280px;
  overflow: auto;
  li {
    padding: 5px 5px 5px 0;
    display: flex;
    justify-content: space-between;
    flex-direction: row;
  }
  .bell {
    display: inline-block;
    width: 20px;
    height: 20px;
    background-image: url('./img/bell.png');
    background-repeat: no-repeat;
    background-size: 20px 20px;
    vertical-align: bottom;
    margin-right: 8px;
  }
  .notice-msg {
    width: 100% - 40px;
    color: #00a0e9;
    flex: 1;
    cursor: pointer;
  }
  .readed {
    color: #c3c3c3;
  }
  .el-pagination-style {
    margin-top: 10px;
    position: absolute;
    bottom: 0;
    float: right;
  }
}
</style>
